package cn.wolfcode.luowowo.util;


import cn.wolfcode.luowowo.exception.LogicException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * 统一异常类
 */
@ControllerAdvice
public class UtilsException {
    //贴标签捕捉指定的异常
    @ExceptionHandler(LogicException.class)
    @ResponseBody
    public Object displayExp(Exception e , HttpServletResponse response){
        //异常信息
        e.printStackTrace();
        //返回的是json类型 将错误信息也带过去 然后前端接收异常信息报错
        return new JsonResult(JsonResult.CODE_ERROR_PARAM,e.getMessage(),null);
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object runTimeExp(Exception e , HttpServletResponse response){
        //异常信息
        e.printStackTrace();
        //返回的是json类型 将错误信息也带过去 然后前端接收异常信息报错
        return JsonResult.defaultError();
    }
}
